<?php
/**
 * 获取分享的URL 最终来的来源<br>
 * 如：http://news.qq.com/a/20110108/000460.htm 最后结果为：qq.com
 * @param unknown_type $url
 */
function getShareUrlSource($url){
	$pattern = "/[\w-]+\.(com|net|org|gov|cc|biz|info|cn|tv|name|asia|tw|hk|in|la|me)(\.(cn|hk))*/";
	preg_match($pattern, $url, $matches);
	if(empty($matches))
		return'';
	if(count($matches) > 0) {
		return $matches[0];
	}else{
		$rs = parse_url($url);
		$main_url = $rs["host"];
		if(!strcmp(long2ip(sprintf("%u",ip2long($main_url))),$main_url)) {
			return $main_url;
		}else{
			$arr = explode(".",$main_url);
			$count=count($arr);
			$endArr = array("com","net","org","3322");
			if (in_array($arr[$count-2],$endArr)){
				 $domain = $arr[$count-3].".".$arr[$count-2].".".$arr[$count-1];
			}else{
				$domain =  $arr[$count-2].".".$arr[$count-1];
			}
			return $domain;
		}
	}
}




function mk_select($data,$v,$name,$key=null)
{
	$value='value';
	$title='title';
	if(!empty($key))
	{
		$value = $key['value'];
		$title =$key['title'];
	}
	$str='<select name="'.$name.'" id="'.$name.'">';
	for($i=0;$i<count($data) ;$i++)
	{
		if($v!==false && $v==$data[$i][$value])
			$str.='<option value="'.$data[$i][$value].'" selected="selected">'.$data[$i][$title].'</option>';
		else
			$str.='<option value="'.$data[$i][$value].'">'.$data[$i][$title].'</option>';
	}
	return $str.='</select>';
}

//输出成功消息
function alert($msg,$url='')
{
	echo $msg;
	exit;
}



/**
 * 格式化时间
 * @param int $time
 */
function diyGetTime($time){
	$now	=	time();
	$c	 =	 $now-$time;

	if($c<5)
		return'刚刚';
	elseif($c<60)
		return $c.'秒前';
	elseif($c<3600)
		return floor($c/60).'分钟前';
	elseif($c<86400)
		return floor($c/3600).'小时前';
	else
		return floor($c/86400).'天前';

}

?>